package de.fstyle.android.location;

import java.util.Date;

import de.fstyle.android.location.startstop.GpsStartStopActivity;
import de.fstyle.android.location.startstop.NetworkStartStopActivity;
import de.fstyle.android.location.startstop.PassiveStartStopActivity;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TableLayout;

public class MainActivity extends Activity {

	public final String LOG_TAG = "location_main_activity";

	public static final UpdateLocationListener GPS_LISTENER = 
			new UpdateLocationListener();
	public static final UpdateLocationListener NETWORK_LISTENER = 
			new UpdateLocationListener();
	public static final UpdateLocationListener PASSIVE_LISTENER = 
			new UpdateLocationListener();
	
	private TableLayout timeTable;
	private TableLayout gpsTable;
	private TableLayout networkTable;
	private TableLayout passiveTable;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.timeTable = LayoutHelper.createTable("time", this);
		
		this.gpsTable = LayoutHelper.createTable("gps", this);
		this.networkTable = LayoutHelper.createTable("network", this);
		this.passiveTable = LayoutHelper.createTable("passive", this);

		LinearLayout linearLayout = 
				(LinearLayout) findViewById(R.id.provider_container);
		linearLayout.addView(this.timeTable);
		linearLayout.addView(this.gpsTable);
		linearLayout.addView(this.networkTable);
		linearLayout.addView(this.passiveTable);
		
		updateLastKnownLocations();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		Log.d(LOG_TAG, "dispatch touch event: " + ev);
		
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
				updateLastKnownLocations();
			break;

		default:
			break;
		}
		
		return super.dispatchTouchEvent(ev);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		Intent intent;
		switch (item.getItemId()) {
		case R.id.action_gps:
			intent = new Intent(this, GpsStartStopActivity.class);
			break;
		case R.id.action_network:
			intent = new Intent(this, NetworkStartStopActivity.class);
			break;
		case R.id.action_passive:
			intent = new Intent(this, PassiveStartStopActivity.class);
			break;
		default:
			intent = null;
			break;
		}
		this.startActivity(intent);
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.d(LOG_TAG, "on touch event: " + event.getAction());
		return false;
	}
	
	private void updateLastKnownLocations() {

		Log.d(LOG_TAG, "updating last location");

		LayoutHelper.messageRow(
				this.timeTable, LayoutHelper.DATE_FORMAT.format(new Date()));
		
		LocationManager locationManager = (LocationManager)
				this.getSystemService(LOCATION_SERVICE);

		getLocationInformation(
				LocationManager.NETWORK_PROVIDER, 
				locationManager, 
				this.networkTable, 
				false);
		
		getLocationInformation(
				LocationManager.GPS_PROVIDER, 
				locationManager, 
				this.gpsTable, 
				false);
		
		getLocationInformation(
				LocationManager.PASSIVE_PROVIDER, 
				locationManager, 
				this.passiveTable, 
				true);
	}
	
	public static void getLocationInformation(
			String provider,
			LocationManager locationManager,
			TableLayout tableLayout,
			boolean showProvider) {
		
		if (!locationManager.isProviderEnabled(provider)) {
			LayoutHelper.messageRow(tableLayout, "location provider is disabled");
			return;
		}
		
		try {
			Location location = locationManager.getLastKnownLocation(provider);
			LayoutHelper.updateRow(tableLayout, location, showProvider);
		} catch (IllegalArgumentException e) {
			LayoutHelper.messageRow(tableLayout, "location provider does not exist");
		}
	}

}
